package com.lagou.demo.util;

import lombok.*;

import java.io.Serializable;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1l;

    private Integer code;
    private String msg;
    private T result;


    public static Result success() {
        return new Result().builder()
                .code(Code.SUCCESS.getCode())
                .msg(Code.SUCCESS.getMsg())
                .build();
    }

    public static Result success(Object obj) {
        return new Result().builder()
                .code(Code.SUCCESS.getCode())
                .msg(Code.SUCCESS.getMsg())
                .result(obj)
                .build();
    }

    public static Result fail() {
        return new Result().builder()
                .code(Code.FAIL.getCode())
                .msg(Code.FAIL.getMsg())
                .build();
    }

    public static Result fail(String msg) {
        return new Result().builder()
                .code(Code.FAIL.getCode())
                .msg(msg)
                .build();
    }

    public static Result fail(String msg, Object obj) {
        return new Result().builder()
                .code(Code.FAIL.getCode())
                .msg(msg)
                .result(obj)
                .build();
    }

    @AllArgsConstructor
    @Getter
    enum Code {
        SUCCESS(200, "执行成功"),
        FAIL(500, "执行失败")
        ;
        private Integer code;
        private String msg;

    }

}
